/**
 * There are <a href="https://www.hdzhx.com">邯郸智讯</a> code generation
 */
package com.boxin.ims.modules.wechat.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 微信业务Entity
 * @author Jakemanse
 * @version 2013-10-23
 */
@Entity
@Table(name = "wp_business")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class WechatBusiness extends BaseEntity {
	
	private static final long serialVersionUID = 1L;
	private Long id; 		// 编号
	private User user; 		// 用户
	private Date createTime;	// 创建日期
	private String delFlag;	// 删除标记（0：正常；1：删除）
	
	private List<BusinessConfig> businessConfigList = Lists.newArrayList(); // 服务列表

	public WechatBusiness() {
		this.createTime = new Date();
		this.delFlag = DEL_FLAG_NORMAL;
	}

	public WechatBusiness(Long id){
		this();
		this.id = id;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	//@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_wechat_wechatBusiness")
	//@SequenceGenerator(name = "seq_wechat_wechatBusiness", sequenceName = "seq_wechat_wechatBusiness")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name="user_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	

	
	@NotNull
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Length(min=1, max=1)
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}
	
	
	//多对多定义
		@ManyToMany(fetch = FetchType.EAGER)
		@JoinTable(name = "wp_business_to_user", joinColumns = { @JoinColumn(name = "user_business_id") }, inverseJoinColumns = { @JoinColumn(name = "business_config_id") })
		@Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'")
		@OrderBy("id") @Fetch(FetchMode.SUBSELECT)
		@NotFound(action = NotFoundAction.IGNORE)
		@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
		public List<BusinessConfig> getBusinessConfigList() {
			return businessConfigList;
		}

		public void setBusinessConfigList(List<BusinessConfig> businessConfigList) {
			this.businessConfigList = businessConfigList;
		}
		
		@Transient
		public List<Long> getBusinessConfigIdList() {
			List<Long> businessConfigIdList = Lists.newArrayList();
			for (BusinessConfig config : businessConfigList) {
				businessConfigIdList.add(config.getId());
			}
			return businessConfigIdList;
		}

		@Transient
		public void setBusinessConfigIdList(List<Long> businessConfigIdList) {
			businessConfigList = Lists.newArrayList();
			for (Long configId : businessConfigIdList) {
				BusinessConfig config = new BusinessConfig();
				config.setId(configId);
				businessConfigList.add(config);
			}
		}
	
}


